package com.caiying.dota.action;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.caiying.dota.domain.User;
import com.caiying.dota.net.Respone;
import com.caiying.dota.net.Status;
import com.caiying.dota.service.UserService;
import com.caiying.dota.util.EncriptUtil;
import com.caiying.dota.util.JsonUtil;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opensymphony.xwork2.ActionContext;

@Controller @Scope("prototype")
public class UserAction {
	@Resource UserService userSerciveBean;
	private File imageFile;
	
	public File getImageFile() {
		return imageFile;
	}

	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}

	private User user;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	//////////////////////////////
	// some useful method for handle user
	//////////////////////////////
	public void signup(){
		String avatar_dir_path = ServletActionContext.getServletContext().getRealPath("/avatar");
		File avatar_dir = new File(avatar_dir_path);
		if(!avatar_dir.exists()){
			avatar_dir.mkdirs();
		}
		String avatar = user.getUsername() + "_avatar";
		user.setAvatar(avatar_dir.getAbsolutePath() + "/" +avatar);
		System.out.println(user.getAvatar());
//		String realPath = ServletActionContext.getServletContext().getRealPath("/images");
		File saveFile = new File(avatar_dir, avatar);
		try {
			FileUtils.copyFile(imageFile, saveFile);
		} catch (IOException e) {
			e.printStackTrace();
		}  
		String message = "no result";
		if(user != null){
			if(user.getUsername().equals("") || user.equals("") || user.getNickname().equals("")){
				message = JsonUtil.toString(new Respone(Status.ERROR, 300, "username, password, and nickname cannot be null"));
			}else{
				boolean result = userSerciveBean.save(user);
				if(result){
					message = JsonUtil.toString(new Respone(Status.OK, 200, "signup success"));
				}else{
					message = JsonUtil.toString(new Respone(Status.ERROR, 301, "signup failed"));
				}
			}
		}
		JsonUtil.outJson(message);
	}
	
	public void signin(){
		String message = "no result";
		if(user != null){
			String username = user.getUsername();
			String salt = String.valueOf(Calendar.getInstance().getTimeInMillis());
			if(username.equals("")){
				message = JsonUtil.toString(new Respone(Status.ERROR, 300, "both username and password cannot be null"));
			}else{
				if(userSerciveBean.get(username) != null){
					String real_passwd = userSerciveBean.get(username).getPassword();
					String encript_passwd = EncriptUtil.encrypt(EncriptUtil.encrypt(real_passwd) + salt);
					Map<String, String> result = new HashMap<String, String>();
					result.put("salt", salt);
					result.put("key", encript_passwd);
					message = JsonUtil.toString(new Respone(Status.OK, 200, result));
				} else {
					message = JsonUtil.toString(new Respone(Status.ERROR, 301, "don't exits this user"));
				}
			}
		}
		JsonUtil.outJson(message);
	}
	
	public String signup_page(){
		return "signup_page";
	}
	
	public String signin_page(){
		return "signin_page";
	}

	public String addUser(){
		userSerciveBean.save(user);
		ActionContext.getContext().put("message", "保存成功");
		return "message";
	}
	
	public String execute(){
		ActionContext.getContext().put("users", userSerciveBean.list());
		return "list";
	}
	
	public String test(){
		return "test";
	}
}
